package cn.iocoder.yudao.module.pplan.controller.admin.tablewaredistplan;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pplan.dal.dataobject.tablewaredistplan.TablewareDistPlanDO;
import cn.iocoder.yudao.module.pplan.service.tablewaredistplan.TablewareDistPlanService;
import cn.iocoder.yudao.module.pplan.controller.admin.tablewaredistplan.vo.TablewareDistPlanPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.tablewaredistplan.vo.TablewareDistPlanRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.tablewaredistplan.vo.TablewareDistPlanSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 生产计划餐具配送计划")
@RestController
@RequestMapping("/pplan/tableware-dist-plan")
@Validated
public class TablewareDistPlanController {

    @Resource
    private TablewareDistPlanService tablewareDistPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建机供品餐具配送计划")
    @PreAuthorize("@ss.hasPermission('acm:tableware-dist-plan:create')")
    public CommonResult<Long> createTablewareDistPlan(@Valid @RequestBody TablewareDistPlanSaveReqVO createReqVO) {
        return success(tablewareDistPlanService.createTablewareDistPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机供品餐具配送计划")
    @PreAuthorize("@ss.hasPermission('acm:tableware-dist-plan:update')")
    public CommonResult<Boolean> updateTablewareDistPlan(@Valid @RequestBody TablewareDistPlanSaveReqVO updateReqVO) {
        tablewareDistPlanService.updateTablewareDistPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机供品餐具配送计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('acm:tableware-dist-plan:delete')")
    public CommonResult<Boolean> deleteTablewareDistPlan(@RequestParam("id") Long id) {
        tablewareDistPlanService.deleteTablewareDistPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除机供品餐具配送计划")
                @PreAuthorize("@ss.hasPermission('acm:tableware-dist-plan:delete')")
    public CommonResult<Boolean> deleteTablewareDistPlanList(@RequestParam("ids") List<Long> ids) {
        tablewareDistPlanService.deleteTablewareDistPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机供品餐具配送计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('acm:tableware-dist-plan:query')")
    public CommonResult<TablewareDistPlanRespVO> getTablewareDistPlan(@RequestParam("id") Long id) {
        TablewareDistPlanDO tablewareDistPlan = tablewareDistPlanService.getTablewareDistPlan(id);
        return success(BeanUtils.toBean(tablewareDistPlan, TablewareDistPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机供品餐具配送计划分页")
    @PreAuthorize("@ss.hasPermission('acm:tableware-dist-plan:query')")
    public CommonResult<PageResult<TablewareDistPlanRespVO>> getTablewareDistPlanPage(@Valid TablewareDistPlanPageReqVO pageReqVO) {
        PageResult<TablewareDistPlanRespVO> pageResult = tablewareDistPlanService.getTablewareDistPlanPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机供品餐具配送计划 Excel")
    @PreAuthorize("@ss.hasPermission('acm:tableware-dist-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTablewareDistPlanExcel(@Valid TablewareDistPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TablewareDistPlanRespVO> list = tablewareDistPlanService.getTablewareDistPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机供品餐具配送计划.xls", "数据", TablewareDistPlanRespVO.class,
                        BeanUtils.toBean(list, TablewareDistPlanRespVO.class));
    }

}